<template>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="800px" append-to-body>
        <el-form ref="form" :model="model" :rules="rules" label-width="80px" label-suffix=":" class="expert-class" style="min-height: 350px" v-loading="loading">
            <el-form-item label="标签" prop="tags">
                <el-input 
                        v-model.trim="newTag" 
                        placeholder="输入新标签并按回车创建"
                        @keyup.enter.native="handleCreateTag"
                        clearable
                        style="width: 600px;"
                    />
                <div class="tag-list" style="margin-top: 12px;">
                    <el-checkbox-group v-model="model.tags" style="width: 600px;">
                        <el-checkbox 
                            v-for="(tag, index) in tagList" 
                            :key="index" 
                            :label="tag.tag_name"
                            style="margin-right: 8px;"
                        >
                            {{ tag.tag_name }}
                        </el-checkbox>
                    </el-checkbox-group>
                </div>
            </el-form-item>
            <el-form-item label="标签描述" prop="tag_desc" style="opacity: 0;">
                <el-input v-model.trim="model.tag_desc" placeholder="请输入标签描述" clearable style="width: 600px;" />
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" :loading="save" :disabled="save" @click="submit">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>
</template>

<script>
import { editExpertTags, getTags } from '@/api/expert'
export default {
    name: 'TagsDetailDialog',
    props: {
        value: {
            type: Boolean,
            default: false,
            required: true,
        },
        params: {
            type: Object,
            default: () => {},
        },
    },
    data() {
        return {
            loading: false,
            save: false,
            model: {
                id: this.params.id,
                tags: this.params?.tags?.split(",")??[],
            },
            rules: {},
            tagList: [],
            newTag: '',
        }
    },
    computed: {
        title() {
            return `编辑标签`
        },
        dialogVisible: {
            get() {
                return this.value
            },
            set(val) {
                this.$emit('input', val)
            },
        },
    },

    async created() {
        try {
            const result = await getTags()
            this.tagList = (result?.data ?? []).filter(item=>item.tag_name)
        } catch (error) {
            console.log(error)
            this.loading = false
        }
    },

    methods: {
        handleCreateTag() {
            if (this.newTag) {
                const newTagObj = {
                    id: this.newTag,
                    tag_name: this.newTag
                }
                this.tagList.push(newTagObj)
                this.model.tags.push(this.newTag)
                this.newTag = ''
            }
        },
        submit() {
            this.$refs.form.validate(valid => {
                if (!valid) {
                    return
                }

                const params = { ...this.model }
                params.tags = params.tags.join(',')

                console.log(params)
                if (this.save) {
                    return
                }
                this.save = true
                editExpertTags(params)
                    .then(res => {
                        this.$message.success(res.msg)
                        this.dialogVisible = false
                        this.$emit('success')
                    })
                    .finally(() => {
                        this.save = false
                    })
            })
        },
        cancel() {
            this.dialogVisible = false
        },
    },
}
</script>

<style>

</style>
